<template>
	<view class="content">
		<view class="top-block">
			<view class="iconfont icon-tishi"><text class="txt">请保证本人对信息内容的真实性和完整性。</text></view>
		</view>
		<view class="info-block">
			<view class="info-item" v-if="type==1">
				<view class="title-item">
					<view class="must">*</view>
					<view class="title">所在村</view>
				</view>
				<view class="form-content">
					<!-- <MultipleColPicker class="form-ctrl" :range="township" :value="villageId" @selected="bindTownship">
					</MultipleColPicker> -->
					<uni-data-picker :localdata="township" @change="onchange" v-model="form.villageId"
						v-if="auditStatus==1||auditStatus==4" @nodeclick="onnodeclick"></uni-data-picker>
					<uni-data-picker :localdata="township" @change="onchange" v-model="form.villageId" v-else
						@nodeclick="onnodeclick" readonly="true"></uni-data-picker>
				</view>
			</view>
			<view class="info-item">
				<view class="title-item">
					<view class="must">*</view>
					<view class="title">姓名</view>
				</view>
				<input placeholder="请输入姓名" v-model="form.name" v-if="auditStatus==1||auditStatus==4" />
				<input placeholder="请输入姓名" v-model="form.name" disabled="true" v-else />
			</view>
			<view class="info-item" v-if="type==1">
				<view class="title-item">
					<view class="must">*</view>
					<view class="title">性别</view>
				</view>
				<view class="form-content">
					<picker :range="gender" range-key="value" :value="form.sex" @change="bindGender" class="box-btn"
						v-if="auditStatus==1||auditStatus==4">
						{{genderIndex==-1 ? "请选择性别" : gender[genderIndex]['value']}}
					</picker>
					<picker :range="gender" range-key="value" :value="form.sex" @change="bindGender" class="box-btn"
						v-else disabled="true">
						{{genderIndex==-1 ? "请选择性别" : gender[genderIndex]['value']}}
					</picker>
					<view class="iconfont icon-gengduo1 arrow"></view>
				</view>
			</view>
			<view class="info-item">
				<view class="title-item">
					<view class="must">*</view>
					<view class="title">手机号码</view>
				</view>
				<input placeholder="请输入手机号码" v-model="form.phoneNumber" type="number" maxlength="11"
					v-if="auditStatus==1||auditStatus==4" />
				<input placeholder="请输入手机号码" v-model="form.phoneNumber" type="number" maxlength="11" disabled="true"
					v-else />
			</view>
			<view class="info-item">
				<view class="title-item">
					<view class="must">*</view>
					<view class="title">身份证号码</view>
				</view>
				<input placeholder="请输入身份证号码" v-model="form.idCard" type="idcard" maxlength="18"
					v-if="auditStatus==1||auditStatus==4" />
				<input placeholder="请输入身份证号码" v-model="form.idCard" type="idcard" maxlength="18" v-else
					disabled="true" />
			</view>
		</view>
		<view class="btn-item" v-if="type==1" @click="submit">
			<view class="btn">提交</view>
		</view>
		<view class="btn-item" @click="application" v-else>
			<view class="btn">立即报名</view>
		</view>
		<SuccessDialog v-if="showDialog" cancelText="返回首页" @callback="dialogCallback">
			<slot style="display: flex;flex-direction: column;">
				<image src="/static/images/shopping/illustration.png"
					style="width: 100%;height: 400rpx;margin-bottom: 50rpx;">
				</image>
				<view style="color: #157DFA;font-size: 38rpx;margin-bottom: 30rpx;"
					class="FangZhengHanZhenGuangBiaoJianTi">提交成功</view>
				<view style="color: #000;line-height: 54rpx; font-size: 30rpx;">
					您的报名提交成功，管理员会在1-3个工作日内审核，请耐心等待。</view>
			</slot>
		</SuccessDialog>
	</view>
</template>

<script>
	import {
		applicationApi
	} from '@/api/activity.js'
	import {
		updateUserInfoApi,
		getInfoApi,
		getselectTreeApi
	} from '@/api/mine.js'
	import {
		validate
	} from '@/util/validate.js'
	import {
		idCardValidate
	} from '@/util/idCardValidate';
	import SuccessDialog from '@/components/SuccessDialog/SuccessDialog.vue';
	import MultipleColPicker from '@/components/Picker/MultipleColPicker'
	import {
		getStorage,
		setStorage
	} from '@/util/auth.js';
	export default {
		components: {
			SuccessDialog,
			MultipleColPicker
		},
		data() {
			return {
				showDialog: false,
				genderIndex: -1,
				auditStatus: 0,
				form: {
					name: '',
					phoneNumber: '',
					idCard: '',
					villageId: [],
					sex: '',
				},
				rules: {
					villageId: {
						required: true,
						name: '所属乡镇村组',
					},
					sex: {
						required: true,
						name: '性别',
					},
					name: {
						name: '姓名',
						required: true,
						validator: function(value, form, callback) {
							if (value.length < 2) {
								callback('请填写正确的姓名')
							}
							callback();
						}
					},
					phoneNumber: {
						name: '手机号码',
						required: true,
						validator: function(value, form, callback) {
							const phoneReg =
								/^((\+|00)86)?((134\d{4})|((13[0-3|5-9]|14[1|5-9]|15[0-9]|16[2|5|6|7]|17[0-8]|18[0-9]|19[0-2|5-9])\d{8}))$/;

							if (value.length != 11) {
								callback('请输入正确的手机号');
							} else if (!phoneReg.test(value)) {
								callback('请输入正确的手机号');
							}
							callback();
						}
					},
					idCard: {
						name: '身份证号',
						required: true
					}
				},
				type: null,
				id: 0,
				state: null,
				gender: [{
						id: 1,
						value: '男'
					},
					{
						id: 2,
						value: '女'
					}
				],
				township: [],
				villageId: []
			}
		},
		methods: {
			navigate(url) {
				uni.navigateTo({
					url
				})
			},
			onchange(e) {
				this.villageId = e.detail.value;
				if (this.villageId.length > 0) {
					this.form.villageId = this.villageId[2].value;
				}
			},
			onnodeclick(node) {},

			bindTownship(township) {
				this.form.villageId = township
				this.villageId = township
			},
			bindGender(e) {
				this.genderIndex = e.detail.value;
				this.form.sex = this.gender[this.genderIndex].id
			},
			dialogCallback(e) {
				uni.switchTab({
					url: '/pages/index/index'
				})
			},
			application() {
				uni.showLoading({
					title: '提交中',
					mask: true
				});
				let form = JSON.parse(JSON.stringify(this.form));
				if (validate(form, this.rules)) {
					const idcard = idCardValidate(this.form.idCard);
					if (!idcard.result) {
						this.showToast('请输入正确身份证号码', () => {});
						return;
					}
					applicationApi({
						name: this.form.name,
						phoneNumber: this.form.phoneNumber,
						idCard: this.form.idCard,
						activityId: this.id,
						villageId: this.form.villageId
					}).then(res => {
						uni.hideLoading()
						this.showDialog = true
					}).catch(() => {
						uni.hideLoading()
					})
				}
			},
			submit() {
				uni.showLoading({
					title: '提交中',
					mask: true
				});
				let form = JSON.parse(JSON.stringify(this.form));
				if (validate(form, this.rules)) {
					const idcard = idCardValidate(this.form.idCard);
					if (!idcard.result) {
						this.showToast('请输入正确身份证号码', () => {});
						return;
					}
					updateUserInfoApi({
						name: this.form.name,
						phoneNumber: this.form.phoneNumber,
						idCard: this.form.idCard,
						villageId: this.form.villageId,
						sex: this.form.sex
					}).then(res => {
						uni.hideLoading()
						uni.showToast({
							title: '提交成功',
							icon: 'success',
							duration: 1000,
							mask: true,
							success: function() {
								setTimeout(function() {
									uni.switchTab({
										url: '/pages/mine/mine'
									})
								}, 1000)
							},
						})
					}).catch(() => {
						uni.hideLoading()
					})
				}
			},
			getDetail() {
				uni.showLoading({
					title: '加载中',
					mask: true
				})
				getInfoApi({}).then(res => {
					uni.hideLoading()
					this.form = Object.assign(this.form, res)
					this.genderIndex = res.sex - 1
					if (this.form.cityId !== 0) {
						this.township.forEach((item, index) => {
							if (item.id == res.cityId) {
								this.villageId.push(index)
							}
							item.childList.forEach((item1, index1) => {
								if (item1.id == res.townId) {
									this.villageId.push(index1)
								}
								item1.childList.forEach((item2, index2) => {
									if (item2.id == res.villageId) {
										this.villageId.push(index2)
									}
								})
							})
						})
						this.form.villageId = this.villageId
					} else
						this.form.villageId = []
				}).catch(() => {
					uni.hideLoading()
				})
			},
			getselectTree() {
				uni.showLoading({
					title: '加载中',
					mask: true
				})
				getselectTreeApi({}).then(res => {
					uni.hideLoading()
					const data = res
					data.forEach((item, index) => {
						item.value = item.id
						item.text = item.name
						delete(item.id)
						delete(item.name)
						item.children.forEach((item1, index1) => {
							item1.value = item1.id
							item1.text = item1.name
							delete(item1.id)
							delete(item1.name)
							item1.children.forEach((item2, index2) => {
								item2.value = item2.id
								item2.text = item2.name
								delete(item2.id)
								delete(item2.name)
							})
						})
					})
					this.township = data;
				}).catch(() => {
					uni.hideLoading()
				})
			}
		},
		onLoad(e) {
			console.log(e)
			this.getselectTree()
			this.id = e.id
			this.type = e.type
			uni.setNavigationBarTitle({
				title: e.name
			})
			this.getDetail()
			this.auditStatus = e.auditStatus
		}
	}
</script>

<style lang="scss">
	.input-value-border {
		width: 430rpx;
		border: none !important;
		display: flex !important;
		justify-content: space-between !important;
	}

	.placeholder {
		color: #000 !important;
		font-size: 30rpx !important;
	}

	.item {
		padding: 0 30rpx !important;
		height: 80rpx !important;
	}

	.content {
		width: 100%;
		padding: 30rpx;

		.top-block {
			background: #CADFFA;
			padding: 30rpx;
			border-radius: 20rpx;
			align-items: center;

			.iconfont {
				color: #127BEC;
				font-size: 44rpx;
				display: flex;
				align-items: center;

				.txt {
					font-size: 30rpx;
					margin-left: 10rpx;
				}
			}
		}

		.info-block {
			width: 100%;
			margin-top: 30rpx;
			background: #fff;
			padding: 30rpx;
			border-radius: 20rpx;
			display: flex;
			flex-direction: column;

			.info-item {
				display: flex;
				align-items: center;
				line-height: 120rpx;
				border-bottom: 1px solid #eee;
				width: 100%;

				.title-item {
					display: flex;
					align-items: center;
					width: 220rpx;

					.must {
						color: #ED3F3D;
					}
				}

				.form-content {
					width: calc(100 - 220rpx);

					.form-ctrl {
						overflow: hidden;
						text-overflow: ellipsis;
						white-space: nowrap;
					}
				}

			}

			.info-item:last-child {
				border-bottom: none;
			}
		}

		.btn-item {
			width: calc(100% - 60rpx);
			display: flex;
			justify-content: center;
			margin-top: 60rpx;

			.btn {
				background: #157DFA;
				color: #fff;
				width: 340rpx;
				height: 80rpx;
				border-radius: 60rpx;
				display: flex;
				align-items: center;
				justify-content: center;
			}
		}
	}
</style>